﻿using Microsoft.VisualStudio.Modeling;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace Project.Co_DataTransformationPrototype
{
    public static partial class Operators
    {
        public partial class Operator
        {

            private bool checkRemoveOperator<T>(IList<T> elementCollection)
            {
                Debug.Assert(opStore != null);

                // TODO: Implement precondition check
                return true;
            }

            public Operator RemoveOperator<T>(IList<T> elementCollection)
            {
                checkRemoveOperator(elementCollection);

                if (elementCollection != null)
                {
                    if (elementCollection.OfType<EntityShape>().Count() > 0)
                        removeEntity(elementCollection.OfType<EntityShape>().ToList());
                    else if (elementCollection.OfType<Entity>().Count() > 0)
                        removeEntity(elementCollection.OfType<Entity>().ToList());
                    else if (elementCollection.OfType<EntityAttribute>().Count() > 0)
                        removeAttribute(elementCollection.OfType<EntityAttribute>().ToList());
                    else if (elementCollection.OfType<EntityAssociation>().Count() > 0)
                        removeLink(elementCollection.OfType<EntityAssociation>().ToList());
                }

                return this;
            }

            private void removeEntity<T>(IList<T> entityList)
            {
                // Transaction is required if you want to update elements.
                using (Transaction t = opStore.TransactionManager.BeginTransaction("Remove Element"))
                {
                    Entity element;
                    foreach (T shape in entityList)
                    {
                        if (typeof(EntityShape) == shape.GetType())
                            element = (shape as EntityShape).ModelElement as Entity;
                        else
                            element = shape as Entity;
                        element.Delete();
                    }
                    t.Commit();
                }
            }

            private void removeAttribute<T>(IList<T> attributeList)
            {
                // Transaction is required if you want to update elements.
                using (Transaction t = opStore.TransactionManager.BeginTransaction("Remove Attribute"))
                {
                    foreach (T attribute in attributeList)
                    {
                        (attribute as EntityAttribute).Delete();
                    }
                    t.Commit();
                }
            }

            private void removeLink<T>(List<T> relationshiplist)
            {
                // Transaction is required if you want to update elements.
                using (Transaction t = opStore.TransactionManager.BeginTransaction("Remove Link"))
                {
                    foreach (T rel in relationshiplist)
                    {
                        (rel as EntityAssociation).Delete();
                    }
                    t.Commit();
                }
            }
        }
    }

}